//
//  PDFViewer.m
//  PDFview
//
//  Created by Антон on 30.11.12.
//  Copyright (c) 2012 Антон. All rights reserved.
//

#import "PDFViewer.h"

@implementation PDFViewer
@synthesize PDFPageView, pathToPDF, img;




/*- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
	return [self.PDFPageView.subviews objectAtIndex:0];
}*/

+(void)initing:(UIScrollView *)scrollView:(NSString *)path{
    PDFViewer* pdfView=[PDFViewer new];
    pdfView.PDFPageView=scrollView;
    pdfView.pathToPDF=path;
    [pdfView setAll];
}
-(void)setAll
{
    
    numberPage=1;
    
    const char *s=[pathToPDF cStringUsingEncoding:NSUTF8StringEncoding];
    CFStringRef path=CFStringCreateWithCString(NULL, s, kCFStringEncodingUTF8);
    CFURLRef url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle,0);
    doc= CGPDFDocumentCreateWithURL(url);
    CGPDFPageRef page;
    page=CGPDFDocumentGetPage(doc, numberPage);
    CGSize size = CGSizeMake(self.PDFPageView.frame.size.width,self.PDFPageView.frame.size.height);
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextTranslateCTM(context, 0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextFillRect(context, self.PDFPageView.bounds);
    
    CGContextSaveGState(context);
	CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, self.PDFPageView.frame.size.width,self.PDFPageView.frame.size.height), 0, true);
	CGContextConcatCTM(context, pdfTransform);
    CGContextDrawPDFPage (context, page);
  	UIImageView *tempImageView =
    [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
	img.center = self.PDFPageView.center;
    self.img = tempImageView;
    
	[tempImageView release];
    CGSizeMake(img.frame.size.width, img.frame.size.height);
	PDFPageView.maximumZoomScale = 4.0;
	PDFPageView.minimumZoomScale = 1.0;
	PDFPageView.clipsToBounds = YES;
    PDFPageView.canCancelContentTouches=NO;

	[PDFPageView addSubview:img];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
    
    [PDFPageView addGestureRecognizer:singleTap];
    
}





- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    CGPoint touchPoint=[gesture locationInView:PDFPageView];
    if ((touchPoint.x<self.PDFPageView.frame.size.width/2)&&(numberPage>1)&&(PDFPageView.zoomScale==1)){
        
        numberPage=numberPage-1;
    }
    
    if ((touchPoint.x>self.PDFPageView.frame.size.width/2)&&(numberPage<CGPDFDocumentGetNumberOfPages(doc))&&(PDFPageView.zoomScale==1)){
        numberPage=numberPage+1;
    }
    
    PDFPageView.zoomScale=1.0;
    CGPDFPageRef page;
    page=CGPDFDocumentGetPage(doc, numberPage);
    
    CGSize size = CGSizeMake(self.PDFPageView.frame.size.width,self.PDFPageView.frame.size.height);
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextTranslateCTM(context, 0, 1000);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextFillRect(context, self.PDFPageView.bounds);
    CGContextSaveGState(context);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, self.PDFPageView.frame.size.width,self.PDFPageView.frame.size.height), 0, true);
    CGContextConcatCTM(context, pdfTransform);
    CGContextDrawPDFPage (context, page);
    
    img.image= UIGraphicsGetImageFromCurrentImageContext();
    
    [PDFPageView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [PDFPageView addSubview:img];
    
    
}






- (void)dealloc {
    
    [pathToPDF release];
    [img release];
    [PDFPageView release];
    [super dealloc];
}
/*
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    [self.PDFPageView reloadInputViews];
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
@end
